package org.lex.jdbc.url.parser;

import org.lex.jdbc.url.IJdbcUrlContext;
import org.lex.jdbc.url.Variable;

public class VariableFragment extends Fragment {
	/**
	 * 
	 * @param image
	 *            like "${varName}"
	 */
	private String variableName = null;

	public VariableFragment(String image) {
		super(image);
		this.variableName = this.parseVariableName(image);
	}

	@Override
	public String evaluate(IJdbcUrlContext context) {
		Variable var = context.getVariable(variableName);
		return context.getVariableValue(var);
	}

	public String getVariableName() {
		return variableName;
	}

	private String parseVariableName(String image) {
		int from = image.indexOf('{');
		int to = image.indexOf('}');
		return image.substring(from + 1, to);
	}
}
